// Webapp 主要封装了接口请求方法
var Webapp = {};

// Ajax - POST
Webapp.postLoadData = function(url, data, success, error) {
	var timeOut = true;
	if (data && (typeof(data) == "object")) {
		data.subtime = new Date().getTime();
	} else {
		data = {
			subtime: (new Date().getTime())
		};
	}
	setTimeout(function() {
		if (timeOut && error) error();
	}, 10000);
	$.ajax({
		"url": url,
		"data": data,
		"type": "get",
		"success": function(rtnData) {
			timeOut = false;
			if (rtnData.status == 302) {
				if (rtnData.desc) {
					baseTool.popup(rtnData.desc);
				}

			} else if (rtnData.status == 200) {
				if (success) {
					success(rtnData);
				}
			} else {
				if (error) {
					error(rtnData);
				} else if (rtnData.desc) {
					baseTool.popup(rtnData.desc);
				}

			}
		},
		"error": function(rtnData, t, tt) {
			timeOut = false;
			if (error) {
				error(rtnData)
			} else {
				baseTool.popup('服务繁忙');
			}
		},
		dataType: "json"
	});
}

// Ajax - GET
Webapp.loadJsonData = function(url_src, success, err_handle) { //调用JSON数据
	var timeOut = false;
	var ok = false;
	$.getJSON(url_src, function(data) {
		if (!timeOut) {
			ok = true;
			success(data);
		}
	});

	setTimeout(function() {
		if (!ok) {
			timeOut = true;
			if (err_handle) err_handle(0);
		}
	}, 10000);
}

/**
 * [setCookie]
 * @param {[type]} name  [cookie name]
 * @param {[type]} value [cookie value]
 * @param {[type]} p     [格式如下（没有则默认为null）
 * 	                     {"expires": [cookie存在的时间，可以是"session" "hour" "day" "week" "year" "forever" 或 毫秒数或 Date对象],
 * 		                  "path":null,
 * 		                   "domain":null,
 * 		                  "secure":null }]
 */
Webapp.setCookie = function(name, value, p) {
	var sCookie = name + '=' + encodeURIComponent(value);
	if (p) {
		if (p.expires) {
			if (p.expires != "session") {
				var etime = new Date();
				if (p.expires instanceof Date) {
					etime = p.expires
				} else if (!isNaN(p.expires)) {
					etime.setTime(etime.getTime() + p.expires)
				} else if (p.expires == "hour") {
					etime.setHours(etime.getHours() + 1);
				} else if (p.expires == "day") {
					etime.setDate(etime.getDate() + 1)
				} else if (p.expires == "week") {
					etime.setDate(etime.getDate() + 7);
				} else if (p.expires == "year") {
					etime.setFullYear(etime.getFullYear() + 1);
				} else if (p.expires == "forever") {
					etime.setFullYear(etime.getFullYear() + 120);
				} else {
					etime = p.expires
				}
				sCookie += "; expires=" + etime.toGMTString();
			}
		}

		if (p.path) {
			sCookie += "; path=" + p.path;
		} else {
			sCookie += "; path=/";
		}

		if (p.domain) {
			sCookie += "; domain=" + p.domain;
		} else {
			sCookie += "; domain=" + window.location.host;
		}

		if (p.secure) {
			sCookie += "; secure=" + p.secure;
		}
	}

	document.cookie = sCookie;
}

/**	
 * [getCookie]
 * @param  {[type]} objname [cookie name]
 * @return {[type]} value   [cookie value]
 */
Webapp.getCookie = function(objname) {
	var arrstr = document.cookie.split("; ");
	for (var i = 0; i < arrstr.length; i++) {
		var temp = arrstr[i].split("=");
		if (temp[0] == objname) return decodeURIComponent(temp[1]);
	}

	return null;
}

Webapp.adsFollow = function() {
	
	if (getUrlParams(window.location.href, "utm_source")) {
		var url = window.location.href;
		
		var args = {
			"method": "adsFollow",
			"utmSource": getUrlParams(url, "utm_source"),
			"utmMedium": getUrlParams(url, "utm_medium"),
			"utmTerm": getUrlParams(url, "utm_term"),
			"utmContent": getUrlParams(url, "utm_content"),
			"utmCampaign": getUrlParams(url, "utm_campaign"),
			"utmCookie": getUrlParams(url, "utm_cookie")
		};

		Webapp.postLoadData('/business.do', args,
			function(data) {},
			function(e) {}
		);
	}
	
}

// 封装一些可复用的方法
var Common = function() {};

Common.prototype = {
	_dectectBroswer: function() {
		var userAgent = navigator.userAgent,
			result = '';

		if (userAgent.match(/Chrome/i)) {
			result = 'Chrome';
		} else if (userAgent.match(/Firefox/i)) {
			result = 'Firefox';
		} else if (userAgent.match(/Mobile\/[0-9A-z]{6,10} Safari/i)) {
			result = 'Mobile Safari';
		} else if (userAgent.match(/Android/i)) {
			result = 'Android';
		} else if (userAgent.match(/ucweb/i)) {
			result = 'UCWeb';
		} else if (userAgent.match(/MQQBrowser/i)) {
			result = 'QQBrowser';
		} else if (userAgent.match(/Windows Phone/i)) {
			result = 'Windows Phone';
		} else {
			result = 'Other';
		}

		return result;
	},
	_dectectAppLink: function() {
		var userAgent = navigator.userAgent,
			appLink = '';
		var whichChannel = getUrlParams(window.location.href, 'channelid') || Webapp.getCookie('channelid');
		if (userAgent.match(/iPhone|iPad/i)) {
			if (userAgent.indexOf("MicroMessenger") > -1) {
				appLink = "http://um0.cn/8ArgH/";
				//appLink = "http://a.app.qq.com/o/simple.jsp?pkgname=cn.touna.touna";
			} else if (whichChannel == 'A00001' || whichChannel == 'A00002') {
				appLink = 'http://um0.cn/4tGsa9/';
			} else if (whichChannel == 'A00003' || whichChannel == 'A00004') {
				appLink = 'http://um0.cn/WRCIr/';
			} else if (whichChannel == 'A00005' || whichChannel == 'A00006') {
				appLink = 'http://um0.cn/4dZc0r/';
			} else if (whichChannel == 'C00002') {
				appLink = 'http://um0.cn/1FL9sp';
			} else if (whichChannel == 'C00003') {
				appLink = 'http://um0.cn/3NkLtw';
			} else if (whichChannel == 'C00004') {
				appLink = 'http://um0.cn/1Gl6Bv';
			} else if (whichChannel == 'A00009') {
				appLink = 'http://um0.cn/3Iajfn/';
			} else if (whichChannel == 'C00005') {
				appLink = 'http://um0.cn/3MzSUG/';
			} else if (whichChannel == 'C00006') {
				appLink = 'hhttp://um0.cn/2xU6xH/';
			} else if (whichChannel == 'A00010') {
				appLink = 'http://um0.cn/3lJiQG/';
			} else if (whichChannel == 'C00007') {
				appLink = 'http://um0.cn/409d78/';
			} else if (whichChannel == 'C00010') {
				appLink = 'http://um0.cn/1LVo60/';
			} else if (whichChannel == 'A00027') {
				appLink = 'http://um0.cn/2LrK0k';
			} else if (whichChannel == 'A00022') {
				appLink = 'http://um0.cn/1oaqVY';
			} else if (whichChannel == 'A00021') {
				appLink = 'http://um0.cn/1UsPnq';
			} else if (whichChannel == 'A00026') {
				appLink = 'http://um0.cn/4ivDxy';
			} else if (whichChannel == 'A00024') {
				appLink = 'http://um0.cn/3GUiYm';
			} else if (whichChannel == 'A00023') {
				appLink = 'http://um0.cn/42jhMb';
			} else if (whichChannel == 'A00025') {
				appLink = 'http://um0.cn/1FQVrN';
			} else if (whichChannel == 'A00028') {
				appLink = 'http://um0.cn/1JDUq';
			} else if (whichChannel == 'A00008') {
				appLink = 'http://um0.cn/1ipmLI';
			} else if (whichChannel == 'A00031') {
				appLink = 'http://um0.cn/374aRt';
			} else if (whichChannel == 'A00029') {
				appLink = 'http://um0.cn/1gFqAH';
			} else if (whichChannel == 'C00012') {
				appLink = 'http://um0.cn/3Jybkw';
			} else if (whichChannel == 'C00013') {
				appLink = 'http://um0.cn/15vAQx';
			} else if (whichChannel == 'C00015') {
				appLink = 'http://um0.cn/1qbUuH';
			} else if (whichChannel == 'C00016') {
				appLink = 'http://um0.cn/1CrgIB';
			} else if (whichChannel == 'C00017') {
				appLink = 'http://um0.cn/2QmEr7';
			} else if (whichChannel == 'C00018') {
				appLink = 'http://um0.cn/2PaDPq';
			} else if (whichChannel == 'C00019') {
				appLink = 'http://um0.cn/1XDWq7';
			} else if (whichChannel == 'C00032') {
				appLink = 'http://um0.cn/75W5D';
			} else if (whichChannel == 'C00020') {
				appLink = 'http://um0.cn/qFVc';
			} else if (whichChannel == 'C00021') {
				appLink = 'http://um0.cn/3jVWg0';
			} else if (whichChannel == 'C00022') {
				appLink = 'http://um0.cn/uhZKx';
			} else if (whichChannel == 'C00023') {
				appLink = 'http://um0.cn/1AGBw4';
			} else if (whichChannel == 'C00024') {
				appLink = 'http://um0.cn/4zxOfa';
			} else if (whichChannel == 'C00025') {
				appLink = 'http://um0.cn/1ZvLZl';
			} else if (whichChannel == 'C00026') {
				appLink = 'http://um0.cn/1Vjh1V';
			} else if (whichChannel == 'C00027') {
				appLink = 'http://um0.cn/dPdNn';
			} else if (whichChannel == 'A00033') {
				appLink = 'http://um0.cn/UgqpT';
			} else if (whichChannel == 'A00034') {
				appLink = 'http://um0.cn/1dbD2N';
			} else if (whichChannel == 'A00035') {
				appLink = 'http://um0.cn/4tQHg4';
			} else if (whichChannel == 'A00036') {
				appLink = 'http://um0.cn/3htYmy';
			} else {
				appLink = 'http://um0.cn/8ArgH';
			}
		} else if (userAgent.match(/Android/i)) {
			if (whichChannel == 'A00001' || whichChannel == 'A00002') {
				appLink = 'http://file.touna.cn/app/touna_licai_bdss.apk';
			} else if (whichChannel == 'A00003' || whichChannel == 'A00004') {
				appLink = 'http://file.touna.cn/app/touna_licai_360ss.apk';
			} else if (whichChannel == 'A00005' || whichChannel == 'A00006') {
				appLink = 'http://file.touna.cn/app/touna_licai_sgss.apk';
			} else if (whichChannel == 'C00002') {
				appLink = 'http://file.touna.cn/app/touna_licai_zaker.apk';
			} else if (whichChannel == 'C00003') {
				appLink = 'http://file.touna.cn/app/touna_licai_ckxx.apk';
			} else if (whichChannel == 'C00004') {
				appLink = 'http://file.touna.cn/app/touna_licai_hsp.apk';
			} else if (whichChannel == 'A00009') {
				appLink = 'http://file.touna.cn/app/touna_licai_ttkd.apk';
			} else if (whichChannel == 'C00005') {
				appLink = 'http://file.touna.cn/app/touna_licai_bmsh.apk';
			} else if (whichChannel == 'C00006') {
				appLink = 'http://file.touna.cn/app/touna_licai_wxdh.apk';
			} else if (whichChannel == 'A00010') {
				appLink = 'http://file.touna.cn/app/touna_licai_by1.apk';
			} else if (whichChannel == 'C00007') {
				appLink = 'http://file.touna.cn/app/touna_licai_wy.apk';
			} else if (whichChannel == 'C00010') {
				appLink = 'http://file.touna.cn/app/touna_licai_by2.apk';
			} else if (whichChannel == 'A00027') {
				appLink = 'http://file.touna.cn/app/touna_licai_pcxm.apk';
			} else if (whichChannel == 'A00022') {
				appLink = 'http://file.touna.cn/app/touna_licai_sm.apk';
			} else if (whichChannel == 'A00021') {
				appLink = 'http://file.touna.cn/app/touna_licai_gdt.apk';
			} else if (whichChannel == 'A00026') {
				appLink = 'http://file.touna.cn/app/touna_licai_google.apk';
			} else if (whichChannel == 'A00024') {
				appLink = 'http://file.touna.cn/app/touna_licai_bdwm.apk';
			} else if (whichChannel == 'A00023') {
				appLink = 'http://file.touna.cn/app/touna_licai_360wm.apk';
			} else if (whichChannel == 'A00025') {
				appLink = 'http://file.touna.cn/app/touna_licai_sgwm.apk';
			} else if (whichChannel == 'A00028') {
				appLink = 'http://file.touna.cn/app/touna_licai_pcsh.apk';
			} else if (whichChannel == 'A00008') {
				appLink = 'http://file.touna.cn/app/touna_licai_bdcf.apk';
			} else if (whichChannel == 'A00031') {
				appLink = 'http://file.touna.cn/app/touna_licai_by5.apk';
			} else if (whichChannel == 'A00029') {
				appLink = 'http://file.touna.cn/app/touna_licai_by3.apk';
			} else if (whichChannel == 'C00012') {
				appLink = 'http://file.touna.cn/app/touna_licai_by4.apk';
			} else if (whichChannel == 'C00013') {
				appLink = 'http://file.touna.cn/app/touna_licai_by6.apk';
			} else if (whichChannel == 'C00015') {
				appLink = 'http://file.touna.cn/app/touna_licai_by9.apk';
			} else if (whichChannel == 'C00016') {
				appLink = 'http://file.touna.cn/app/touna_licai_by8.apk';
			} else if (whichChannel == 'C00017') {
				appLink = 'http://file.touna.cn/app/touna_licai_yowifi.apk';
			} else if (whichChannel == 'C00018') {
				appLink = 'http://file.touna.cn/app/touna_licai_by13.apk';
			} else if (whichChannel == 'C00019') {
				appLink = 'http://file.touna.cn/app/touna_licai_by14.apk';
			} else if (whichChannel == 'C00032') {
				appLink = 'http://file.touna.cn/app/touna_licai_by15.apk';
			} else if (whichChannel == 'C00020') {
				appLink = 'http://file.touna.cn/app/touna_licai_by16.apk';
			} else if (whichChannel == 'C00021') {
				appLink = 'http://file.touna.cn/app/touna_licai_mmy.apk';
			} else if (whichChannel == 'C00022') {
				appLink = 'http://file.touna.cn/app/touna_licai_xm.apk';
			} else if (whichChannel == 'C00023') {
				appLink = 'http://file.touna.cn/app/touna_licai_jf.apk';
			} else if (whichChannel == 'C00024') {
				appLink = 'http://file.touna.cn/app/touna_licai_lx.apk';
			} else if (whichChannel == 'C00025') {
				appLink = 'http://file.touna.cn/app/touna_licai_mm.apk';
			} else if (whichChannel == 'C00026') {
				appLink = 'http://file.touna.cn/app/touna_licai_gdt.apk';
			} else if (whichChannel == 'C00027') {
				appLink = 'http://file.touna.cn/app/touna_licai_by24.apk';
			} else if (whichChannel == 'A00033') {
				appLink = 'http://file.touna.cn/app/touna_licai_by17.apk';
			} else if (whichChannel == 'A00034') {
				appLink = 'http://file.touna.cn/app/touna_licai_by18.apk';
			} else if (whichChannel == 'A00035') {
				appLink = 'http://file.touna.cn/app/touna_licai_by19.apk';
			} else if (whichChannel == 'A00036') {
				appLink = 'http://file.touna.cn/app/touna_licai_sgpz.apk';
			} else {
				appLink = 'http://file.touna.cn/app/touna_licai_gwwap.apk';
			}
		} else {
			appLink = 'http://file.touna.cn/app/touna_licai_gwwap.apk';
		}

		return appLink;
	},
	_getTime: function() {
		var date = new Date();
		var month = '';
		var day = '';

		if ((date.getMonth() + 1) < 10) {
			month = '0' + (date.getMonth() + 1).toString();
		} else {
			month = (date.getMonth() + 1).toString();
		}

		if (date.getDate() < 10) {
			day = '0' + date.getDate().toString();
		} else {
			day = date.getDate().toString();
		}

		date = date.getFullYear() + '-' + month + '-' + day;

		return date;
	},
	_checkNull: function(value) {
		if (value == '' || value == null) {
			return false;
		} else {
			return true;
		}
	},
	_checkZhName: function(value) {
		var reg = /^[\u4e00-\u9fa5a]{2,6}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true
		}
	},
	_checkRegisName: function(value) {
		var reg = /^([a-zA-Z])([a-zA-Z0-9_-]){3,19}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	_checkLoginName: function(value) {
		var reg = /^(13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}|18[0-9]{9})|([a-zA-Z0-9_-]|[\u4e00-\u9fa5a]){2,16}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	_checkPw: function(value) {
		var reg = /^(?![^a-zA-Z]+$)(?!\D+$).{6,20}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	_checkPhone: function(value) {
		var reg = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}|18[0-9]{9}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true
		}
	},
	_checkCode: function(value) {
		var reg = /^[0-9a-zA-Z]{4}|[0-9]{6}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true
		}
	},
	_checkCardId: function(value) {
		var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	_checkEmail: function(value) {
		var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	//空格判断
	_checkSpace: function(value) {
		var reg = /\s/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
	//QQpanduan  
	_checkqq: function(value) {
		var reg = /^[1-9]\d{4,14}$/;
		if (reg.exec(value) == null) {
			return false;
		} else {
			return true;
		}
	},
}

var common = new Common();

// 获取url参数 - REFFER->rapidborrowing-stock.js
function getUrlParams(url, n) {
	var hrefstr, pos, parastr, para, tempstr;
	hrefstr = url;
	pos = hrefstr.indexOf("?");
	parastr = hrefstr.substring(pos + 1);
	para = parastr.split("&");
	tempstr = "";
	for (var i = 0; i < para.length; i++) {
		tempstr = para[i];
		pos = tempstr.indexOf("=");
		if (tempstr.substring(0, pos).toLowerCase() == n.toLowerCase()) {
			return decodeURIComponent(tempstr.substring(pos + 1));
		}
	}
	return "";
}

//对JSON数据做处理 并绑定响应的点击事件
function redchuli(r) {
	//0红点是否显示

	if (r != '') {
		var redcheck = r[0].redcheck;
		var redN = redcheck.trim().split(',');
		for (var i = 0; i < redN.length; i++) {

			//大模块
			var dd = '#' + redN[i];
			$(dd).unbind('click');
			//红点
			var divi = '#h' + redN[i];
			//localkey
			var redtimeo = "redtime" + redN[i];
			hoval(redtimeo, divi);
			redcli(redN[i], redtimeo);
		}
		var sid = Webapp.getCookie('sid');
		if (sid) {
			$('#2,#3').unbind();
			$('#h2,#h3').remove('.hongdian');
		}
	}

}

//dianji事件
function redcli(redN, redtimeo) {
	var $dd = $('#' + redN);
	$dd.data("a", "#h" + redN);
	$dd.data("b", redtimeo);

	$dd.live('click', function() {
		var $this = $(this);
		$($this.data("a")).css('display', 'none');
		localStorage.setItem($this.data("b"), new Date().getTime());
	});

}

/*广告跟踪  */
var xl = function(e) {
		return document.getElementById(e);
	}

$(function(){ // 增加广告追踪
	Webapp.adsFollow();
});

// 通用 下拉框
$(".selectBox").click(function() {
	$("aside", this).toggle();
	$(this).toggleClass("selectBoxOpen");
});

$(".selectBox i").each(function() {
	$(this).click(function(evt) {
		evt.stopPropagation();
		$(this).parent().parent().find("em").text($(this).text());
		$(".selectBox aside").slideUp();
		$(".selectBox").toggleClass("selectBoxOpen");
	})
});
//-------------------------error 提示 aftername 上一个元素  errmess提示语
var errPrompt = function(aftName, errmess) {
	if ($('.warning').length > 0) {
		$('.warning').html(errmess);
	} else {
		$(aftName).after("<aside class='warning'>" + errmess + "</aside>");
	}
}

//------------广告
// 百度统计
$(function() {
	var _hmt = _hmt || [];
	(function() {
		var hm = document.createElement("script");
		hm.src = "//hm.baidu.com/hm.js?877d00ba98baf5c36f459bcf71c90419";
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(hm, s);
	})();
});

var baseTool = {};

//进度加载事件
baseTool.shadeLoad = function() {
	$("body").append('<div class="shade_load" style="width:100%;height:100%;position:fixed;top:0;left:0;z-index:9999;background:rgba(0,0,0,.8) url(img/loading.gif) no-repeat 50% 50%;background-size:120px 80px;"></div>');
}

//一个按钮的弹出框
baseTool.popup = function(popCont) {
	//解除关于确定按钮的所有绑定事件

	$('.confirm').unbind("click");
	if ($('.mask').length > 0 && $('.popup').length > 0) {
		$(".mask,.popup").show();
		$('.wunai span').html(popCont);

	} else {
		$("body").append('<div class="mask"></div><div class="popup modify-phoneNum-pop"><div class = "wunai" style="text-align:center;padding:5px;border-radius:3px;border:0px solid #d9dde2;font-size:14px"><span>' + popCont + '</span></div><div class="btnBox"><button class="btn-noBg btn-blue">确定</button></div></div>');
		$(".popup").css("margin-top", -$('.popup').show().height() / 2);
		$(".mask,.popup").show();
	}

	$(".btn-blue").bind("click", function() {
		$(".mask,.popup").hide();
	});
};
